package com.example.ljw.frescowallpaper.view;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.ljw.frescowallpaper.R;
import com.example.ljw.frescowallpaper.base.BaseActivity;
import com.example.ljw.frescowallpaper.bean.UserBean;
import com.example.ljw.frescowallpaper.config.Constants;
import com.example.ljw.frescowallpaper.utils.BitmapCircleTransformation;
import com.example.ljw.frescowallpaper.utils.BlurTransformation;
import com.example.ljw.frescowallpaper.utils.GlideCircleTransform;
import com.example.ljw.frescowallpaper.utils.GrayscaleTransformation;
import com.example.ljw.frescowallpaper.utils.PreUtils;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.listener.UpdateListener;

public class UserCenterActivity extends BaseActivity {

    private Context mContext = this;
    private Toolbar userCenter_toobar;
    private Drawable imagebg;
    //用户中心背景图
    private ImageView userBg;
    //注销按钮
    private Button mCancellationBtn;
    //显示用户名的textview
    private TextView showusername_usercenter;
    //修改用户名的edittext
    private EditText usercenter_username_edt;
    //用户名
    private String userName;
    //新用户名
    private String newUserName;
    //用户头像地址
    private String userIcon;
    //用户头像imageview
    private ImageView usericon_usercenter;
    //获得objectId
    private String objectId;
    //修改用户名按钮
    private Button editusername_usercenter_button,confirmusername_usercenter_button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //隐藏标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //隐藏状态栏
        //定义全屏参数
        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        //获得当前窗体对象
        Window window = UserCenterActivity.this.getWindow();
        //设置当前窗体为全屏显示
        window.setFlags(flag, flag);
        setContentView(R.layout.activity_user_center);

        intData();
        initView();
        setUserName();
        EditUserName();
        loadUserIcon();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case Constants.KEY_ASK_FOR_ICON_REQUEST:
                if (resultCode == RESULT_OK){
                    Uri uri = data.getData();
                    Glide.with(mContext)
                            .load(uri)
//                            .transform(new BlurTransformation(mContext))
                            .bitmapTransform(new GrayscaleTransformation(mContext),new BlurTransformation(mContext))
                            .into(userBg);

                    Glide.with(mContext)
                            .load(uri)
                            .transform(new GlideCircleTransform(mContext,2,mContext.getResources().getColor(R.color.orange)))
                            .into(usericon_usercenter);

                    UserBean userBean = new UserBean();
                    userBean.setIconUrl(uri.toString());
                    PreUtils.clear(UserCenterActivity.this,Constants.KEY_WITH_ICON);
                    PreUtils.writeString(UserCenterActivity.this,Constants.KEY_WITH_ICON,uri.toString());
                    userBean.update(objectId, new UpdateListener() {
                        @Override
                        public void done(BmobException e) {
                            Toast.makeText(mContext, "更换头像成功", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
                break;
        }
    }

    private void loadUserIcon() {
        usericon_usercenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent,Constants.KEY_ASK_FOR_ICON_REQUEST);
            }
        });
    }

    private void intData() {
        getUserBeanObjectId();

    }

    //获取用户名
    private void setUserName() {
        Intent intent = getIntent();
        userName = intent.getStringExtra("caonima");
        userIcon = intent.getStringExtra("usericon");
        showusername_usercenter.setText(userName);
        Glide.with(mContext)
                .load(userIcon)
//                            .transform(new BlurTransformation(mContext))
                .bitmapTransform(new GrayscaleTransformation(mContext),new BlurTransformation(mContext))
                .into(userBg);

        Glide.with(mContext)
                .load(userIcon)
                .transform(new GlideCircleTransform(mContext,2,mContext.getResources().getColor(R.color.orange)))
                .into(usericon_usercenter);

    }
    //获取objectid
    private void getUserBeanObjectId() {
        objectId = PreUtils.readStrting(UserCenterActivity.this, Constants.KEY_OF_OBJECTID);
    }

    //修改用户名
    private void EditUserName() {
        editusername_usercenter_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                usercenter_username_edt.setVisibility(View.VISIBLE);
                editusername_usercenter_button.setVisibility(View.GONE);
                confirmusername_usercenter_button.setVisibility(View.VISIBLE);
            }
        });


        //确认修改用户名点击监听
        confirmusername_usercenter_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                newUserName = usercenter_username_edt.getEditableText().toString();
                PreUtils.clear(UserCenterActivity.this,Constants.KEY_HAVA_USER);
                PreUtils.writeString(UserCenterActivity.this,Constants.KEY_HAVA_USER,newUserName);
                Toast.makeText(UserCenterActivity.this, objectId, Toast.LENGTH_SHORT).show();
                UserBean userbean = new UserBean();
                userbean.setUserName(newUserName);
                userbean.update(objectId, new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        Toast.makeText(UserCenterActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                    }
                });
                finish();
                Toast.makeText(UserCenterActivity.this, newUserName, Toast.LENGTH_SHORT).show();
            }
        });
    }



    private void initView() {

        ImageView backImg = (ImageView) findViewById(R.id.iv_back);
        backImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        showusername_usercenter = (TextView) findViewById(R.id.showusername_usercenter);
        //头像ImageView
        usericon_usercenter = (ImageView) findViewById(R.id.usericon_usercenter);
        confirmusername_usercenter_button = (Button) findViewById(R.id.confirmusername_usercenter_button);
        editusername_usercenter_button = (Button) findViewById(R.id.editusername_usercenter_button);
        usercenter_username_edt = (EditText) findViewById(R.id.usercenter_username_edt);

        userBg = (ImageView) findViewById(R.id.usercenter_background);
        mCancellationBtn = (Button) findViewById(R.id.cancellation_usercenter);

        //注销按钮，清空并且跳转到登录界面
        mCancellationBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(UserCenterActivity.this,LoginActivity.class);
                PreUtils.clear(UserCenterActivity.this,"Constants.KEY_HAVA_USER");
                startActivity(intent);
                finish();
            }
        });
        //设置用户中心背景图
        userBg.setImageResource(R.mipmap.usercenterbg);
        Glide.with(mContext)
                .load(R.mipmap.usercenterbg)
                .transform(new BlurTransformation(mContext))
                .into(userBg);

//        imagebg = userBg.getDrawable();
//        imagebg.mutate().setAlpha(200);
//        userBg.setImageDrawable(imagebg);

    }

}
